﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib;
using System.IO.Ports;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using WFNetLib.GSMModem;
using WFNetLib.Forms;
using WFNetLib.Net;
using System.IO;
using System.IO.Compression;
using System.Net;

namespace ATReaderTest
{
    public class SMSThread
    {
        public SerialPort Com;
        public Thread ReaderThreadIns=null;
        public static event strEventHandler AddLogEvent;//事件声明
#if DEBUG
        CommunicationDebugForm CommunicationDebugForm;
#endif


        GsmModem gsmModem;
        public string PortName;
        void OnAddLog(string strLog)
        {
            if (AddLogEvent != null)
            {                
                AddLogEvent(this,new strEventArgs(strLog));
            }
        }        
        public SMSThread(SerialPort _Com)
        {
            Com = _Com;
            
#if DEBUG
            CommunicationDebugForm = new CommunicationDebugForm();
            CommunicationDebugForm.Show();
            //CommunicationDebugForm.Hide();
            CommunicationDebugForm.Text = Com.PortName + "数据检测";
            gsmModem = new GsmModem(Com);//gsmModem = new GsmModem(Com,CommunicationDebugForm);
#else
            gsmModem = new GsmModem(Com);
#endif
           

            gsmModem.dIP[0] = 10;
            gsmModem.dIP[1] = 0;
            gsmModem.dIP[2] = 0;
            gsmModem.dIP[3] = 172;
            gsmModem.dPort = 80;
            
//             gsmModem.sIP = new byte[4];
//             gsmModem.sIP[0] = 10;
//             gsmModem.sIP[1] = 0xbd;
//             gsmModem.sIP[2] = 0xf0;
//             gsmModem.sIP[3] = 0xce;
            gsmModem.AddLog += new strEventHandler(gsmModem_AddLog);
            
        }

        void gsmModem_AddLog(object sender, strEventArgs e)
        {
            OnAddLog(e.strArg);
        }
        public void ReaderThreadStart()
        {
            ReaderThreadIns = new Thread(new ThreadStart(ReaderProc));
            ReaderThreadIns.Name = "签到" + Com.PortName;
            PortName = Com.PortName;
            ReaderThreadIns.Start();
            OnAddLog("签到开始");
        }
        private void ReaderProc()
        {
            while(true)
            {
                try
                {
                    Com.Open();                    
                    //Com.RtsEnable = true;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    OnAddLog("串口" + Com.PortName + "无法打开");
                    break;
                }
                if (!gsmModem.InitGSMModem())
                {
                    OnAddLog(ReaderThreadIns.Name + "初始化失败");
                    break;
                }                
                OnAddLog(ReaderThreadIns.Name + "初始化成功");
                if (!gsmModem.SendMsg("13009999100", "你好")) 
                {
                    OnAddLog(ReaderThreadIns.Name + "短信发送失败");
                    break;
                }
                break;
            }
            Debug.WriteLine("完成");
            OnAddLog("<未知退出>");
            Com.Close();
        }        
    }
}
